package reviewAlgorithms.dynamicProgramming;

public class WiggleMaxLength {
    public static void main(String[] args) {
        int[] nums = {1,7,4,9,2,2,5};
        System.out.println(wiggleMaxLength(nums));
    }
    public static int wiggleMaxLength(int[] nums) {
        if (nums == null || nums.length < 1){
            return 0;
        }
        if (nums.length < 2) {
            return 1;
        }
        int[] dp = new int[nums.length];
        dp[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            int max = 1;
            int temp = nums[i] - nums[i - 1];
            if (temp == 0){
                dp[i] = dp[i - 1];
                continue;
            }
            for (int j = i - 1; j >= 1; j--) {
                if (temp * (nums[j] - nums[j - 1]) < 0){
                    max = Math.max(Math.max(3,max),dp[j] + 1);
                }
            }
            dp[i] = max;
            if (i == 1){
                dp[i] = 2;
            }
        }
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            max =Math.max(max,dp[i]);
        }
        return max;
    }

}
